.\"	$NetBSD: shmat.2,v 1.18 2010/03/22 19:30:55 joerg Exp $
.\"
.\" Copyright (c) 1995 Frank van der Linden
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"      This product includes software developed for the NetBSD Project
.\"      by Frank van der Linden
.\" 4. The name of the author may not be used to endorse or promote products
.\"    derived from this software without specific prior written permission
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 17, 2002
.Dt SHMAT 2
.Os
.Sh NAME
.Nm shmat ,
.Nm shmdt
.Nd map/unmap shared memory
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/shm.h
.Ft void *
.Fn shmat "int shmid" "const void *shmaddr" "int shmflg"
.Ft int
.Fn shmdt "const void *shmaddr"
.Sh DESCRIPTION
.Fn shmat
maps the shared memory segment associated with the shared memory identifier
.Fa shmid
into the address space of the calling process.
The address at which the segment is mapped is determined by the
.Fa shmaddr
parameter.
If it is equal to 0, the system will pick an address itself.
Otherwise, an attempt is made to map the shared memory segment at the
address
.Fa shmaddr
specifies.
If
.Dv SHM_RND
is set in
.Fa shmflg ,
the system will round the address down to a multiple of
.Dv SHMLBA
bytes
.Pf ( Dv SHMLBA
is defined in
.In sys/shm.h ) .
.Pp
A shared memory segment can be mapped read-only by specifying the
.Dv SHM_RDONLY
flag in
.Fa shmflg .
.Pp
.Fn shmdt
unmaps the shared memory segment that is currently mapped at
.Fa shmaddr
from the calling process' address space.
.Fa shmaddr
must be a value returned by a prior
.Fn shmat
call.
A shared memory segment will remain in existence until it is
removed by a call to
.Xr shmctl 2
with the
.Dv IPC_RMID
command.
.Sh RETURN VALUES
.Fn shmat
returns the address at which the shared memory segment has been mapped into
the calling process' address space when successful,
.Fn shmdt
returns 0 on successful completion.
Otherwise, a value of \-1 is returned, and the global variable
.Va errno
is set to indicate the error.
.Sh ERRORS
.Fn shmat
will fail if:
.Bl -tag -width Er
.It Bq Er EACCES
The calling process has no permission to access this shared memory segment.
.It Bq Er ENOMEM
There is not enough available data space for the calling process to
map the shared memory segment.
.It Bq Er EINVAL
.Fa shmid
is not a valid shared memory identifier.
.Pp
.Fa shmaddr
specifies an illegal address.
.It Bq Er EMFILE
The number of shared memory segments has reached the system-wide limit.
.El
.Pp
.Fn shmdt
will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa shmaddr
is not the start address of a mapped shared memory segment.
.El
.Sh SEE ALSO
.Xr ipcrm 1 ,
.Xr ipcs 1 ,
.Xr mmap 2 ,
.Xr shmctl 2 ,
.Xr shmget 2
.Sh STANDARDS
The
.Nm shmat
and
.Nm shmdt
system calls conform to
.St -xsh5 .
.Sh HISTORY
Shared memory segments appeared in the first release of
.At V .
